import { Component, OnInit,ViewChild } from '@angular/core';
import {ModalDirective} from 'ngx-bootstrap';
import {AddMail,AddDelivery,AddEMS,AddExtraMail,AddExtraDelivery,AddExtraEMS,EditExpress} from '../delivery';
import {AddressService} from '../../../../lib/service/address.service';
import {LocalStorageService} from '../../../../lib/service/local-storage.service';
import {DeliveryManageService} from '../delivery-manage.service'

@Component({
  selector: 'app-delivery-add',
  templateUrl: './delivery-add.component.html',
  styleUrls: ['./delivery-add.component.scss'],
  providers: [DeliveryManageService]
})
export class DeliveryAddComponent implements OnInit {

  type: string;
  message: string;
  isShow = false;

  token;
  provinceList = [];
  cityList = [];
  regionList = [];
  provinceCode = 0;  // 选中省code
  cityCode = 0;
  regionCode = 0;
  delivery_name: string;  // 模板名称
  hasSavedTemp = false;  // 是否已经保存运费模板
  type_mail: boolean;
  type_delivery: boolean;
  type_EMS: boolean;
  mailList = '';
  deliveryList = '';
  EMSList = '';
  addMail = new AddMail;
  addDelivery = new AddDelivery;
  addEMS = new AddEMS;
  addExtraMail = new AddExtraMail;
  addExtraDelivery = new AddExtraDelivery;
  addExtraEMS = new AddExtraEMS;
  editExpress = new EditExpress;
  extraMailRange = [];
  extraDeliveryRange = [];
  extraEMSRange = [];
  editExpressRange = [];
  addProvinceList = [];
  checkedProvince: string; // 当前选中的省名
  selfAdd = [];  // 自定义添加地区
  isEdit: boolean;  // 是否在编辑中添加地区
  operateId: number;  // 操作的模板ID
  operateDefault: boolean;  // 操作的模板是否属于默认模板
  operateType: number;  // 操作的模板类型 0:平邮 1:快递 2:EMS

  express_template_id: number;  // 运费模板ID

  @ViewChild('cityModal') public cityModal: ModalDirective;
  @ViewChild('deleteModal') public deleteModal: ModalDirective;
  @ViewChild('editModal') public editModal: ModalDirective;
  constructor(private addressService: AddressService,
              private localStorageService: LocalStorageService,
              private deliveryManageService: DeliveryManageService) { }

  ngOnInit() {
    this.token = this.localStorageService.getToken();
    this.getProvinceList();
  }

  // 获取省列表
  getProvinceList() {
    this.addressService.getProvinceList()
      .subscribe(
        res => {
          if(res.code === 2000){
            this.provinceList = res.data;
          }else{
            this.type = 'error';
            this.message = res.message;
            this.isShow = true;
          }
        }
      )
  }

  // 选择省之后
  changeProvince() {
    this.cityCode = 0;
    this.regionCode = 0;
    this.regionList = [];
    this.addressService.getCityList(this.provinceCode)
      .subscribe(
        res => {
          if(res.code === 2000){
            this.cityList = res.data;
          }else{
            this.type = 'error';
            this.message = res.message;
            this.isShow = true;
          }
        }
      )
  }

  // 选择市之后
  changeCity(){
    this.regionCode = 0;
    this.addressService.getRegionList(this.cityCode)
      .subscribe(
        res => {
          if(res.code === 2000){
            this.regionList = res.data;
          }else{
            this.type = 'error';
            this.message = res.message;
            this.isShow = true;
          }
        }
      )
  }

  // 下一步
  nextStep(){
    let id = this.localStorageService.getNowApplet().applet_uuid;
    const data = {name: this.delivery_name,applet_uuid: id,where_goods_at: this.regionCode};
    this.deliveryManageService.addExpressTemp(data)
      .subscribe(
        res => {
          if (res.code === 2000) {
            this.express_template_id = res.data.id;
            this.hasSavedTemp = true;
          } else {
            this.type = 'error';
            this.message = res.message;
            this.isShow = true;
          }
        }
      )
  }

  // 获取运费模板内容
  getExpressTempList(typeStr: string,container: string,type: string){
    this.deliveryManageService.getExpressTempValList(this.express_template_id,typeStr).subscribe(
      res => {
        this[container] = res.data.data;
        if(!!res.data.data.length){
          this[type] = true;
          this[container].every((val, idx, array) => {
            val.starting_weight_price = val.starting_weight_price/1000;
            val.exceed_weight_price = val.exceed_weight_price/1000;
            return true;
          })
        }
      }
    )
  }

  // 提交平邮模板内容
  tempMailSubmit(T: boolean) {
    if(!T){
      this.type = 'info';
      this.message = '请将信息填写完整';
      this.isShow = true;
      return;
    }
    this.addMail.express_template_id = this.express_template_id;
    this.deliveryManageService.addExpressTempVal(this.addMail).subscribe(
      res => {
        if(res.code === 2000){
          this.type = 'success';
          this.message = '添加成功';
          this.isShow = true;
          this.getExpressTempList("平邮","mailList","type_mail");
        }else{
          this.type = 'error';
          this.message = res.message;
          this.isShow = true;
        }
      }
    )
  }

  // 提交快递模板内容
  tempDeliverySubmit(T: boolean){
    if(!T){
      this.type = 'info';
      this.message = '请将信息填写完整';
      this.isShow = true;
      return;
    }
    this.addDelivery.express_template_id = this.express_template_id;
    this.deliveryManageService.addExpressTempVal(this.addDelivery)
      .subscribe(
        res => {
          if(res.code === 2000){
            this.type = 'success';
            this.message = '添加成功';
            this.isShow = true;
            this.getExpressTempList("快递","deliveryList","type_delivery");
          }else{
            this.type = 'error';
            this.message = res.message;
            this.isShow = true;
          }
        }
      )
  }

  // 提交EMS模板内容
  tempEMSSubmit(T: boolean){
    if(!T){
      this.type = 'info';
      this.message = '请将信息填写完整';
      this.isShow = true;
      return;
    }
    this.addEMS.express_template_id = this.express_template_id;
    this.deliveryManageService.addExpressTempVal(this.addEMS)
      .subscribe(
        res => {
          if(res.code === 2000){
            this.type = 'success';
            this.message = '添加成功';
            this.isShow = true;
            this.getExpressTempList("EMS","EMSList","type_EMS");
          }else{
            this.type = 'error';
            this.message = res.message;
            this.isShow = true;
          }
        }
      )
  }

  // 提交指定地区的模板内容
  tempExtraSubmit(type: number){
    let data;
    if(type === 0 ){
      this.addExtraMail.express_template_id = this.express_template_id;
      if(!this.extraMailRange.length){
        this.type = 'info';
        this.message = '请选择配送区域';
        this.isShow = true;
        return;
      }
      this.addExtraMail.range = JSON.stringify(this.extraMailRange);
      data = this.addExtraMail;
    }
    if(type === 1 ){
      this.addExtraDelivery.express_template_id = this.express_template_id;
      if(!this.extraDeliveryRange.length){
        this.type = 'info';
        this.message = '请选择配送区域';
        this.isShow = true;
        return;
      }
      this.addExtraDelivery.range = JSON.stringify(this.extraDeliveryRange);
      data = this.addExtraDelivery;
    }
    if(type === 2 ){
      this.addExtraEMS.express_template_id = this.express_template_id;
      if(!this.extraEMSRange.length){
        this.type = 'info';
        this.message = '请选择配送区域';
        this.isShow = true;
        return;
      }
      this.addExtraEMS.range = JSON.stringify(this.extraEMSRange);
      data = this.addExtraEMS;
    }
    this.deliveryManageService.addExpressTempVal(data)
      .subscribe(
        res => {
          if(res.code === 2000){
            this.type = 'success';
            this.message = '添加成功';
            this.isShow = true;
            this.getExpressTempList("平邮","mailList","type_mail");
            this.getExpressTempList("快递","deliveryList","type_delivery");
            this.getExpressTempList("EMS","EMSList","type_EMS");
          }else{
            this.type = 'error';
            this.message = res.message;
            this.isShow = true;
          }
        }
      )
  }
  // 添加
  addSelf(){
    for(let i = 0;i < this.selfAdd.length;i++){
      if(this.checkedProvince === this.selfAdd[i]){
        return;
      }
    }
    this.selfAdd.push(this.checkedProvince);
  }

  addCityDialog(type: number,isEdit?: boolean){
    this.operateType = type;
    this.addressService.getProvinceList()
      .subscribe(
        res => {
          if(res.code === 2000){
            if(this.operateType === 0){
              this.selfAdd = this.extraMailRange;
            }
            if(this.operateType === 1){
              this.selfAdd = this.extraDeliveryRange;
            }
            if(this.operateType === 2){
              this.selfAdd = this.extraEMSRange;
            }
            if(isEdit){
              this.isEdit = true;
              this.selfAdd = this.editExpressRange;
            }else{
              this.isEdit = false;
            }
            this.addProvinceList = res.data;
            this.cityModal.show();
          }
        }
      )
  }

  // 删除模板内容
  delValDialog(id: number){
    this.operateId = id;
    this.deleteModal.show();
  }
  delSubmit(){
    this.deliveryManageService.delExpressTempVal(this.operateId)
      .subscribe(
        res => {
          if(res.code === 2000){
            this.type = 'success';
            this.message = '删除成功';
            this.isShow = true;
            this.deleteModal.hide();
            this.getExpressTempList("平邮","mailList","type_mail");
            this.getExpressTempList("快递","deliveryList","type_delivery");
            this.getExpressTempList("EMS","EMSList","type_EMS");
          }else{
            this.type = 'error';
            this.message = res.message;
            this.isShow = true;
          }
        }
      )
  }

  // 编辑弹窗
  editDialog(id: number,isDefault: number){
    this.operateId = id;
    this.operateDefault = !isDefault;
    this.deliveryManageService.getExpressTempValDetail(id).subscribe(
      res => {
        if(res.code === 2000){
          this.editExpress = res.data;
          this.editExpress.starting_weight_price = this.editExpress.starting_weight_price/1000;
          this.editExpress.exceed_weight_price = this.editExpress.exceed_weight_price/1000;
          this.editExpressRange = JSON.parse(res.data.range);
          this.editModal.show();
        }else{
          this.type = 'error';
          this.message = res.message;
          this.isShow = true;
        }

      }
    )
  }
  // 提交编辑表单
  editSubmit(){
    this.editExpress.range = JSON.stringify(this.editExpressRange);
    this.deliveryManageService.editExpressTempVal(this.editExpress)
      .subscribe(
        res => {
          if(res.code === 2000){
            this.type = 'success';
            this.message = '修改成功';
            this.isShow = true;
            this.editModal.hide();
            this.getExpressTempList("平邮","mailList","type_mail");
            this.getExpressTempList("快递","deliveryList","type_delivery");
            this.getExpressTempList("EMS","EMSList","type_EMS");
          }else{
            this.type = 'error';
            this.message = res.message;
            this.isShow = true;
          }
        }
      )
  }

}
